/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

import canvas.MIDPCanvasPlay;
import java.security.spec.X509EncodedKeySpec;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author LTTDNQ
 */
public class RunableItemsFall implements Runnable {
    //Khai báo biến
    //Ví trị của vật phẩm rơi

    private int _viTriItemsX = 120;
    private int _viTriItemsY = 62;
    //Giá trị gán
    private int _tempY;
    private int _vitri;
    private boolean _flagTrung = true;
    private boolean _flagResult = false;
    //Tốc độ từ 1-10 ( Mặc định là 10)
    private MIDPCanvasPlay canvas;
    private Image _img;
    //Tạo các constructor

    public RunableItemsFall(Image img, int x, int y, MIDPCanvasPlay canvas, int vitri, boolean flag) {
        this._img = img;
        _viTriItemsX = x;
        _viTriItemsY = y;
        this.canvas = canvas;
        this._vitri = vitri;
        this._flagTrung = flag;
    }

    public int getViTriItemsX() {
        return _viTriItemsX;
    }

    public void setViTriItemsX(int x) {
        this._viTriItemsX = x;
    }

    public int getViTriItemsY() {
        return _viTriItemsY;
    }

    public void setViTriItemsY(int y) {
        this._viTriItemsY = y;
    }

    public void run() {
       canvas.chayLuongRoiVat();
        do {
            try {

                Thread.sleep(50 *canvas.getLevel());
                diChuyenTrung();
            } catch (Exception ex) {
                System.out.print("lỗi");
            }
        } while (_viTriItemsY < 260);
        canvas.dungLuongRoiVat();
    }

    public void paintItem(Graphics graphics) {
        //Vẽ vật phẩm
        if (!_flagResult) {
            graphics.drawImage(_img, _viTriItemsX, _viTriItemsY, Graphics.LEFT | Graphics.TOP);
        } else {
            canvas.getArrRunableItemsFall()[_vitri] = null;
            canvas.setSoItems(canvas.getSoItems() - 1);
        }
    }
    //Kiểm tra xem có hứng được trứng không
//
//    private void gameOver() {
//        if (canvas.getLife() < 1) {
//            canvas.getMidlet().vaoManHinhKetThuc();
//        }
//    }

    private void kiemTraHungTrung() {

        if (canvas.getToaDoRoX() < _viTriItemsX && canvas.getToaDoRoX() + 40 > _viTriItemsX) {
            int n;
            if (_flagTrung) {

                n = canvas.getScore() + 1;
                if (n % 20 == 0 && canvas.getLevel() > 1) {
                    int n2 = canvas.getLevel() - 1;
                    canvas.setLevel(n2);
                     int l = canvas.getLife() + 1;
                    canvas.setLife(l);
                }
                canvas.setScore(n);
            } else {
                n = canvas.getLife() - 1;
                canvas.setLife(n);
               // gameOver();
            }
            _flagResult = true;

            _viTriItemsY = 350;
            canvas.repaint();

        } else {
            _viTriItemsY = _viTriItemsY + 10;
            if (_viTriItemsY > 250) {
                if (_flagTrung) {
                    int n = canvas.getLife() - 1;
                    canvas.setLife(n);
                    //gameOver();
                }
                _flagResult = true;
                _viTriItemsY = 350;
            }
            canvas.repaint();

        }
    }

    private void diChuyenTrung() {
        //Gán tạm tọa độ vật phẩm lúc đầu
        if (_viTriItemsY < 220) {
            _tempY = _viTriItemsY;
            //Thay đổi tọa độ vật phẩm
            _viTriItemsY = _viTriItemsY + 5;
            //Vẽ lại khu vực thay doo
            canvas.repaint();
        } else {
            kiemTraHungTrung();
        }
    }
}
